package com;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Properties;

public class stupid {
    private static final String sp = File.separator;

    public static HashMap<String, String> makeDocumentHtml(String packagePath) {
        String jarPath = new fly().getJarPath();
        String pFilePath = jarPath + sp + "config" + sp + "cover2.properties";


        Properties properties = new Properties();
        HashMap<String, String> keyMap = new HashMap<>();
        try {
            properties.load(new FileReader(pFilePath));
        } catch (IOException e) {
            e.printStackTrace();
        }
        for (String s : properties.stringPropertyNames()) {
            String s1 = packagePath + sp + properties.get(s);
            s1 = s1.replace("\\", sp);
            s1 = s1.replace("/", sp);
            if (new File(s1).exists()) {
                keyMap.put(s, s1);
            }
        }

        try {
            Configuration configuration = new Configuration(Configuration.getVersion());
            configuration.setDirectoryForTemplateLoading(new File(jarPath + sp + "ftl"));
            configuration.setDefaultEncoding("UTF-8");
            Template template = configuration.getTemplate("cover2" + ".ftl");
            Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(fly.parentDirectoryPath(packagePath) + sp + "web" + sp + "res" + sp + "cover2.html")), StandardCharsets.UTF_8));
            template.process(keyMap, writer);
            writer.close();
        } catch (IOException | TemplateException e) {
            e.printStackTrace();
        }
        HashMap<String, String> result = new HashMap<>();
        result.put("cover2", fly.parentDirectoryPath(packagePath) + sp + "web" + sp + "res" + sp + "cover2.html");
        int ll = 350 + keyMap.size() * 100;
        result.put("acover2Px", ll + "px");
        return result;
    }
}
